package com.wkq.move.module.search.frame.view

import android.content.Context
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import com.wkq.base.frame.mosby.delegate.MvpView
import com.wkq.lib_base.adapter.KtDataBindingAdapter
import com.wkq.lib_base.utlis.AlertUtil
import com.wkq.lib_base.utlis.KeyboardUtils
import com.wkq.lib_base.utlis.StatusBarUtil
import com.wkq.move.R
import com.wkq.move.module.main.ui.adapter.TvShowAdapter
import com.wkq.move.module.search.ui.activity.TvShowSearchActivity
import com.wkq.move.module.tv.ui.activity.UsTvShowActivity
import com.wkq.net.model.TvListsInfo
import com.wkq.net.model.USTVShowInfo

/**
 * 作者: 吴奎庆
 *
 * 时间: 2020/5/23
 *
 * 简介:
 */
class TvShowSearchView : MvpView {

    var mActivity: TvShowSearchActivity
    var searchAdapter: TvShowAdapter? = null

    constructor(mFragment: TvShowSearchActivity) {
        this.mActivity = mFragment
    }

    fun initView() {
        StatusBarUtil.setStatusBarDarkMode(mActivity)
        StatusBarUtil.setColor(mActivity, mActivity.resources.getColor(R.color.color_2b2b2b), 0)
        mActivity.binding.rlCancle.setOnClickListener { mActivity.finish() }
        mActivity.binding.etSearch.setOnEditorActionListener(object :
            TextView.OnEditorActionListener {
            override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {

                    if (!TextUtils.isEmpty(v?.text)) {
                        if (mActivity.presenter != null) {
                            mActivity.presenter.getSearchData(v?.text.toString())
                        }
                        KeyboardUtils.hideSoftInput(mActivity)
                    } else {
                        showMessage("请输入您搜索的内容")
                    }
                    return true
                }
                return false
            }
        })
        searchAdapter = TvShowAdapter(mActivity as Context, R.layout.item_tv_show)
        mActivity.binding.rvContent.layoutManager = GridLayoutManager(mActivity, 3)
        mActivity.binding.rvContent.adapter = searchAdapter

        searchAdapter!!.setOnViewClickListener(object :
            KtDataBindingAdapter.OnAdapterViewClickListener<USTVShowInfo> {
            override fun onViewClick(v: View?, program: USTVShowInfo?) {
                UsTvShowActivity.Companion.newInstance(
                    mActivity as Context,
                    program?.tvSite
                )
            }
        })
    }

    fun showMessage(message: String) {
        if (mActivity == null || mActivity.isDestroyed || TextUtils.isEmpty(message)) return
        AlertUtil.showDeftToast(mActivity, message)
    }

    fun showSearchInfo(info: TvListsInfo?) {
        if (info != null && info.tvList != null) {
            searchAdapter?.removeAllItems()
            searchAdapter?.addItems(info?.tvList.reversed().toMutableList())
        } else {
            showMessage("暂未搜索到数据")
        }

    }

}